$(function(){
	var url = common.url();
	var began = 0;
	var listlen = 10;
	var pid = common.search().pid;
	
	$.ajax({
		url : '/api/PManage/query.json?pId='+ pid,
		type: "POST",
		error: function(request) {
			console.log(request)
		},
		dataType:"json",
		success: function(data) {
			if(data.ret){
				var json = data.data;
				var name = common.maxlen(json.name,24,true);
				$('.xmdtall-name').html(name);
			}else{}
		}
	})
	$('.tan-close').bind('click',function(){
		$('.tandbg').hide();
		$('.tanboxd').hide();
		$('.surd').val('');
		$('.ulnamepac').html('');
		$('.baocuo').html('');
		$('.filed .ulnamepac').html('');
		$('.filed .upbtn span').removeClass('change').html('上传附件');
		$('.updeimg').attr({'src':'assets/front/image/common/nopic.png'})
	});
	$('.addxm-news').bind('click',function(){
		$('.tandbg').show();
		$('.tanboxd1').show();
	})
	
	var jk = 'api/PDynamic/dynamic.json?id='+ pid;
	var loginurl= common.Interface('onlogin')[0]
	$.ajax({
		url : loginurl,
		type: "POST",
		error: function(request) {
			//console.log(request)
		},
		dataType:"json",
		success: function(data) {
			if(data.ret){
				if(data.data == 'UpFile'){
					common.hash('page',fnhash);
				}else{
					//$('.cont').remove()
					$('.youcede').remove();
					$('.header-box').css({'display':'block'});
					$('.boxbg').css({'display':'block'});
					$('.tanbox').css({'display':'none'});
					$('.login-box').css({'display':'block'});
					$('.showboxtitle').html('登陆');
					$('.baocuo').html('此页面需要上传资料账号...');
					
				}
			}
		}
	})
	
	
	function fnhash(hash){
		//分页
		if(common.hashlist().page){
			began = (common.hashlist().page-1)*listlen
		}
		var url = jk +'&offset='+ began +'&limit='+ listlen
		$.ajax({
				url : url,
				type: "POST",
				async: false,
				error: function(request) {
					console.log(request)
				},
				dataType:"json",
				success: function(data) {
					if(data.ret){
						var data = data.data;
						if(data.rows.length>0){
							var json = data;
							var pages =Math.ceil(json.total / listlen);
							common.pagesgn(pages,6);
							$('.xmlist-boxnews').html('');
							var arr = ['初审','审核通过','审核未通过']
							for(var i=0;i<json.rows.length;i++){
								var title = common.maxlen(json.rows[i].title,20,true);
								var html = '<li class="clear">\
									<p class="xmname">'+title+'</p>\
									<p class="xmstatus">'+ arr[json.rows[i].status] +'</p>\
									<div class="caozuo clear">\
									<p class="change " dataid="'+ json.rows[i].pd_id +'">修改</p>\
										<p class="del " dataid="'+ json.rows[i].pd_id +'">删除</p>\
										<a target="_blank" href="'+ json.rows[i].pd_url +'" class="link ">查看链接</a>\
									</div></li>';
								$('.xmlist-boxnews').append(html)
							}
						}else{
							$('.pages').css({'display':'none'});
							$('.xmlist-boxnews').html('');
						}
					}
					//删除
					$('.xmlist-box .del').bind('click',function(){
						var coid = $(this).attr('dataid');
						$.ajax({
							url : '/api/PDynamic/delete.json?pd_id='+ coid,
							type: "POST",
							error: function(request) {
								console.log(request)
							},
							dataType:"json",
							success: function(data) {
								if(data.ret){
									common.hash('page',fnhash);
								}else{
									alert('删除失败');
								}
							}
						})
					});
					//点击修改，显示内容
					$('.xmlist-box .change').bind('click',function(){
						$('.tandbg').show();
						$('.tanboxd2').show();
						var coid = $(this).attr('dataid');
						$.ajax({
							url : '/api/PDynamic/query.json?pd_id='+ coid,
							type: "POST",
							error: function(request) {
								console.log(request)
							},
							dataType:"json",
							success: function(data) {
								if(data.ret){
									var json = data.data;
									$('.forn-up2 input[name="title"]').val(json.title);
									$('.forn-up2 input[name="pd_id"]').val(json.pd_id);
									$('.forn-up2 input[name="pmanage_id"]').val(json.pmanage_id);
									
									$('.forn-up2 input[name="author"]').val(json.author);
									$('.forn-up2 input[name="source"]').val(json.source);
									$('.forn-up2 textarea[name="summary"]').val(json.summary);
									$('.forn-up2 input[name="startTime"]').val(json.startTime);
									$('.forn-up2 .updeimg').attr({'src':json.picture});
									if(json.fiFileName){
										$('.forn-up2 input[name="updateFil1"]').parents('.upbtn').children('span').html('修改').addClass('change');
										$('.forn-up2 input[name="updateFil1"]').parents('.filed').children('.ulnamepac').html(json.fiFileName);
									}
									if(json.seFileName){
										$('.forn-up2 input[name="updateFil2"]').parents('.upbtn').children('span').html('修改').addClass('change');
										$('.forn-up2 input[name="updateFil2"]').parents('.filed').children('.ulnamepac').html(json.seFileName);
									}
									if(json.thFileName){
										$('.forn-up2 input[name="updateFil3"]').parents('.upbtn').children('span').html('修改').addClass('change');
										$('.forn-up2 input[name="updateFil3"]').parents('.filed').children('.ulnamepac').html(json.thFileName);
									}
								}else{}
							}
						})
					})
				}
			})
	}
	
	// 添加
	$('.form-upbox1 .tijaio').bind('click',function(){
		var onf = true;
		$('.form-upbox1 .surd').each(function(i,e){
			if(e.value == ''){
				onf = false;
				$('.baocuo').html('输入框内容不能为空...');
				return false;
			}
		})
		if(onf){
			$('.surfile').each(function(i,e){
				if(e.value !=''){
					onf = false;
				}
			})
			if(!onf){
				$('#form-upbox1').ajaxSubmit({
					url:'/api/PDynamic/save.json',
					type:'POST',
					data:{pmanage_id:pid},
					success:function(data){
						if(data.ret){
							common.hash('page',fnhash);
							$('.tan-close').trigger('click');
						}
					}
				})
			}else{
				$('.baocuo').html('至少上传一个附件...');
			}
		}
	})
	//修改
	$('.forn-up2 .tijaio').bind('click',function(){
		var onf = true;
		$('.forn-up2 .surd').each(function(i,e){
			if(e.value == ''){
				onf = false;
				$('.baocuo').html('输入框内容不能为空...');
				return false;
			}
		});
	
		if(onf){
			$('#form-upbox2').ajaxSubmit({
				url:'/api/PDynamic/update.json',
				type:'post',
				success:function(data){
					if(data.ret){
						common.hash('page',fnhash);
						$('.tan-close').trigger('click');
					}
				}
			})
		}
	})
})
